#include <iostream>
#include "RingQueue.hpp"
#include "Thread.hpp"
using namespace ThreadModule;
using Task = std::function<void()>;
using ring_queue_t = RingQueue<Task>;
void printdata()
{
    std::cout << "hell word" << std::endl;
}
void producer(ring_queue_t &bq, std::string name)
{
    int cnt = 10;
    while (true)
    {
        sleep(2);
        Task t = printdata;
        bq.emquue(t); // 传递任务
        // std::cout<< name << " in: " << cnt << std::endl;
        // cnt++;
    }
}
void consumer(ring_queue_t &bq, std::string name)
{
    while (true)
    {
        int cnt;
        Task tk;
        bq.pop(&tk);
        tk(); // 执行执行任务
        std::cout << name << " is run : task " << std::endl;
    }
}
void Init_Com(std::vector<Thread<ring_queue_t>> *threads_ptr, ring_queue_t &rq, int num, func_t<ring_queue_t> func, const std::string &who)
{
    for (int i = 0; i < num; i++)
    {
        std::string _name = "thread- " + std::to_string(i + 1) + "  " + who;
        threads_ptr->emplace_back(func, rq, _name);
        // threads_ptr->back().Start();
    }
}
void InitProducer(std::vector<Thread<ring_queue_t>> *threads_ptr, ring_queue_t &rq, int num)
{
    Init_Com(threads_ptr, rq, num, producer, "producer");
}
void InitConsumer(std::vector<Thread<ring_queue_t>> *threads_ptr, ring_queue_t &rq, int num)
{
    Init_Com(threads_ptr, rq, num, consumer, "consumer");
}
void StartAllThread(std::vector<Thread<ring_queue_t>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Start();
    }
}
void WaitAllThread(std::vector<Thread<ring_queue_t>> &threads)
{
    for (auto &thread : threads)
    {
        thread.Join();
    }
}
int main()
{
    ring_queue_t *ptr = new ring_queue_t(10);
    std::vector<Thread<ring_queue_t>> threads; // 所有副线程共享ring_queue
    InitProducer(&threads, *ptr, 1);           // 生产者初始化
    InitConsumer(&threads, *ptr, 3);           // 消费者初始化
    StartAllThread(threads);                   // 启动所有副线程
    WaitAllThread(threads);                    // 等待所有副线程
    return 0;
}